package easymusic.baseutility;

import com.easymusic.EasyMusicMainActivity;
import easymusic.baseinterface.*;
import com.easymusic.R;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Notification.Builder;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.AsyncTask;

@SuppressLint("NewApi")
class EasyMusicMediaService extends AbstractService 
			implements IMediaService, OnCompletionListener{
	private class AsynTaskForUI extends AsyncTask<MediaPlayer, Integer, Void>{

		@Override
		protected Void doInBackground(MediaPlayer... params) {
			MediaPlayer player = params[0];
			player.start();
			while (!isCancelled()){
				try{
					while (player.isPlaying()){
						publishProgress(player.getCurrentPosition());
					}
					publishProgress(player.getCurrentPosition());
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			return null;
		}
		
		@Override
		protected void onProgressUpdate (Integer... currentPosition) {
			if (null == m_callback){
				return;
			}
			m_callback.updateState(currentPosition[0], m_eState);

			
		}
		
		@Override
		protected void onCancelled (Void v){
			m_callback.updateState(0, m_eState);
		}
	}
	private MediaPlayer m_player;
	private AsynTaskForUI m_asynTask;
	private State m_eState = State.uninitialized;
	private ICallback m_callback;
	
	public EasyMusicMediaService(){
		
	}

	@Override
	public ErrorCode load(String strMediaFullPath) {
		if (null == m_player){
			m_player = new MediaPlayer();
		}else{
			m_player.stop();
			m_player.release();
			m_player = new MediaPlayer();
		}
		if (null != m_asynTask){
			m_asynTask.cancel(true);
			m_asynTask = null;
		}
		try {
			m_player.setDataSource(strMediaFullPath);
			m_player.prepare();
			m_player.setOnCompletionListener(this);
		} catch (Exception e) {
			e.printStackTrace();
			return ErrorCode.fail;
		}
		m_eState = State.ready;
		return ErrorCode.success;
	}

	@Override
	public ErrorCode play() {
		if (null == m_player){
			return ErrorCode.not_initialized;
		}
		m_asynTask = new AsynTaskForUI();
		m_player.start();
		m_asynTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, m_player);
		m_eState = State.playing;
		return ErrorCode.success;
	}

	@Override
	public void pause() {
		if (isPlaying()){
			m_player.pause();
			m_eState = State.pause;
		}else{
			m_player.start();
			m_eState = State.playing;
		}
	}

	@Override
	public void stop() {
		if (null != m_player){
			m_player.stop();
			m_player.release();
			m_player = null;
		}
		if (null != m_asynTask){
			m_asynTask.cancel(true);
			m_asynTask = null;
		}

		m_eState = State.stop;

	}

	@Override
	public State getState() {
		return m_eState;
	}

	@Override
	public void registerCallback(ICallback callback) {
		m_callback = callback;
	}

	@Override
	public int getDuration() {
		return m_player.getDuration();
	}

	@Override
	public boolean isPlaying() {
		if (null == m_player){
			return false;
		}
		if (m_player.isPlaying()){
			return true;
		}
		
		return false;
	}

	@Override
	public void seek(int iProgress) {
		if (null != m_player){
			m_player.seekTo(iProgress);
		}
	}

	@Override
	public void onCompletion(MediaPlayer player) {
		stop();
	}

	@Override
	public void stopService() {
		stop();
	}

	@Override
	public void startService(EasyMusicAppService service, Context context) {
		// TODO Auto-generated method stub
		
	}
}
